<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-weight: 16">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>属性管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <div style="margin: 0 0 20px 0;">
      <!-- 属性模板选择框 -->
      <el-select v-model="templateId" placeholder="请选择属性模板" style="width: 250px"
                 @change="loadAttributeList">
        <el-option
            v-for="item in attributeTemplateOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <!-- 添加属性按钮 -->
      <el-button type="primary" size="medium" style="margin-left:20px"
                 @click="$router.push('/sys-admin/product/attribute/add-new')">添加属性
      </el-button>
    </div>

    <!-- 属性列表数据-->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="180" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="type" label="属性类型" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="inputType" label="输入类型" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="valueList" label="备选值列表" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="unit" label="计量单位" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" circle
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
      <!-- 编辑属性详情的对话框 -->
    </el-table>
    <el-dialog title="修改属性" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="属性类型" prop="type">
          <el-input v-model="editForm.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="输入类型" prop="inputType">
          <el-input v-model="editForm.inputType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备选值列表" prop="valueList">
          <el-input v-model="editForm.valueList" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="editForm.unit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" prop="sort">
          <el-input v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 属性模板的下拉数据
      attributeTemplateOptions: [],
      // 属性模板ID
      templateId: '',
      // 表格数据
      tableData: [],
      // 属性对话框
      dialogFormVisible: false,
      // 属性的编辑对话框表单
      editForm: {
        id:'',
        name: '',
        description: '',
        type: '',
        inputType: '',
        valueList: '',
        unit: '',
        sort: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        description: [
          {required: false, message: '请输入简介', trigger: 'blur'},
          {min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请输入属性类型', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        inputType: [
          {required: true, message: '请输入输入类型', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        valueList: [
          {required: true, message: '请输入备选值列表', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        unit: [
          {required: false, message: '请输入计量单位', trigger: 'blur'},
          {min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是0~99之间的数字', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // 删除按钮
    openDeleteConfirm(attribute) {
      this.$confirm('此操作将永久删除【' + attribute.id + '-' + attribute.name + '】属性, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(attribute);
      }).catch(() => {

      });
    },
    // 删除
    handleDelete(attribute) {
      let url = 'http://localhost:9180/attributes/' + attribute.id + '/delete';
      console.log('url' + url);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            type: 'success',
            message: '删除属性成功!'
          });
          this.loadAttributeList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAttributeList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    // 打开编辑对话框
    openEditDialog(attribute) {
      let url = 'http://localhost:9180/attributes/' + attribute.id;

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAttributeList();
            }
          });
        }
      })
    },
    // 提交表单数据
    submitEditForm() {
      let url = 'http://localhost:9180/attributes/' + this.editForm.id + '/update';
      let formData = this.qs.stringify(this.editForm);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url,formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改属性成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadAttributeList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAttributeList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      })

    },
    // 加载属性模板列表
    loadAttributeTemplateList() {
      let url = 'http://localhost:9180/attribute-template?queryType=all';

      let localJwt = localStorage.getItem('localJwt');
      console.log('Jwt=' + localJwt);
      this.axios.create({'headers': {'Authorization': localJwt}})
          .get(url).then((response) => {

        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          console.log('data=' + jsonResult.data);
          this.attributeTemplateOptions = jsonResult.data.list;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    // 加载属性列表
    loadAttributeList() {
      let url = 'http://localhost:9180/attributes/list/' + this.templateId;
      let localJwt = localStorage.getItem('localJwt');
      console.log('Jwt=' + localJwt);
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
        }
      })
    }
  },
  mounted() {
    this.loadAttributeTemplateList();
  }
}
</script>